﻿using UnityEngine;
using UnityEngine.AI;

public class EnemyBehaviours : MonoBehaviour
{
    public GameObject tower;
    public NavMeshAgent agent;
    public float reachDistance = 1;

    public GameObject effect;
    [Header("绘制线段的时长，调试用")]
    public float duration=8;

    private void Awake()
    {
        // 用于初始化字段
        if (!tower) tower = GameObject.Find("Tower");
    }

    void Start()
    {
        agent.SetDestination(tower.transform.position);
        agent.stoppingDistance = reachDistance * 0.5f;

        //可视化调试用
        Debug.DrawLine(transform.position, tower.transform.position, Color.blue, duration);
    }

    void Update()
    {

        if (agent.remainingDistance<=reachDistance)
        {
            PlayEffect();
            Destroy(gameObject);
        }
    }

    private void PlayEffect()
    {
        // todo : 生成爆炸特效
        var go = Instantiate<GameObject>(effect);
        go.transform.position = transform.position;
    }

    private void Reset()
    {
        tower = GameObject.Find("Tower");
        agent = GetComponent<NavMeshAgent>();
    }
}
